﻿var _component = (function () {
    ko.bindingHandlers.validValue = {
        init: function (element, valueAccessor) {
            var observable = valueAccessor();

            $(element).on("change", function (ev) {
                observable(ev.target.value);
            });
        },
        update: function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());

            $(element).removeClass("inputerror");
            valueAccessor().hasError = ko.observable(false);
            var currentErrorState = Main.Validation.ValidateByHTML5(element, value);

            if (currentErrorState) {
                $(element).addClass("inputerror");
                valueAccessor().hasError(true);
            }

            $(element).val(value);
        }
    };

    ko.bindingHandlers.datepicker = {
        init: function (element, valueAccessor) {
            var observable = valueAccessor();

            $(element).datepicker({
                format: 'dd-mm-yyyy'
            }).on("changeDate", function (ev) {
                observable(ev.date);
                $(element).datepicker("hide");
            });

            $(element).on("change", function (ev) {
                if (ev.target.value == "") {
                    observable(ev.target.value);
                }
            });
        },
        update: function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());

            $(element).removeClass("inputerror");
            valueAccessor().hasError = ko.observable(false);
            var currentErrorState = Main.Validation.ValidateByHTML5(element, $(element).val());

            if (currentErrorState) {
                $(element).addClass("inputerror");
                valueAccessor().hasError(true);
            }

            $(element).datepicker("setDate", value);
        }
    };

    return {

    };
})();